package com.lovcreate.filter;

import com.lovcreate.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2016/7/21.
 */
public class UserLoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = (String)request.getParameter("username");
        String password = (String)request.getParameter("password");
        String authority = (String)request.getParameter("authority");

        if("1".equals(authority) && "haoboy".equals(username) && "123".equals(password) || "2".equals(authority) && "gly".equals(username) && "456".equals(password)) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setAuthority(authority);

            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            request.getRequestDispatcher("success.jsp").forward(request,response);
            return ;
        }

        System.out.println("登录失败");

        response.sendRedirect("login.jsp?error=true&username=" +username+ "&authority=" +authority);
    }
}
